# -*- coding: utf-8 -*-
"""
@Time ： 2020/11/2 21:02
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：qq滑动验证码识别
"""
import base64
import random
import time

import pyautogui
from selenium import webdriver
import requests
import cv2
from selenium.webdriver import ActionChains

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
driver.get('https://passport.jd.com/new/login.aspx')

driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/div/div[3]/a').click()
driver.find_element_by_xpath('//*[@id="loginname"]').send_keys('aa1541561')
driver.find_element_by_xpath('//*[@id="nloginpwd"]').send_keys('34324234')
driver.find_element_by_xpath('//*[@id="loginsubmit"]').click()

while True:
    # 下载背景原图和模块原图
    # 背景图
    ele_target = driver.find_element_by_xpath('//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[1]/div[2]/div[1]/img')
    ele_template = driver.find_element_by_xpath(
        '//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[1]/div[2]/div[2]/img')
    target_src = ele_target.get_attribute('src')
    template_src = ele_template.get_attribute('src')

    # 下载
    target_src = target_src[target_src.find('base64,') + 7:]
    imgdata = base64.b64decode(target_src)
    file = open('./imgs/target.png', 'wb')
    file.write(imgdata)
    file.close()

    template_src = template_src[template_src.find('base64,') + 7:]
    imgdata = base64.b64decode(template_src)
    file = open('./imgs/template.png', 'wb')
    file.write(imgdata)
    file.close()

    w1 = ele_target.size['width']

    # 打开背景图
    target_rgb = cv2.imread('./imgs/target.png')
    w2 = target_rgb.shape[1]
    # 灰度处理
    target_gray = cv2.cvtColor(target_rgb, cv2.COLOR_BGR2GRAY)
    # 打开模板图
    template_rgb = cv2.imread('./imgs/template.png', 0)
    # 获取模板在背景图的位置
    res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_CCOEFF_NORMED)
    # 获取最好和最差的匹配
    value = cv2.minMaxLoc(res)
    x = value[2][0]
    # 计算缩放之后的x距离
    x = int(x * w1 / w2 * 1.25)
    print('左边界：%d' % x)

    # # 滑动
    # action = ActionChains(driver)
    # action.move_to_element(ele_template).perform()
    # action.click_and_hold().move_by_offset(int(x * 0.8), 4).pause(0.4).move_by_offset(int(x * 0.27), -2).pause(0.4).move_by_offset(-int(x * 0.07)+1, 2).release().perform()

    # 是元素在网页上面的左上角点的位置
    print(ele_template.location)
    # 矩形大小
    print(ele_template.size)
    # 获取浏览器网页上方的高度
    offset_y = driver.execute_script('return window.outerHeight - window.innerHeight;')
    # 获取模块在网页的位置
    lo = ele_template.location
    # 获取模块的长宽
    sz = ele_template.size
    # 计算在屏幕上面的真实像素位置
    lo['y'] += offset_y
    lo['x'] = int(lo['x']*1.25) + sz['width']//2
    lo['y'] = int(lo['y'] * 1.25) + sz['height']//2

    # pyautogui把鼠标移动到模块元素的中心位置
    pyautogui.moveTo(lo['x'],lo['y'])
    # 按住鼠标
    pyautogui.mouseDown()
    # 拖动鼠标
    # 起始位置和目标位置的连线移动的
    pyautogui.moveTo(lo['x']+x,lo['y'],duration=0.8)
    # 松开鼠标
    pyautogui.mouseUp()

    time.sleep(2)
    try:
        driver.implicitly_wait(3)
        ele = driver.find_element_by_xpath('//div[@class="JDJRV-img-refresh"]')
        ele.click()
        time.sleep(1)
    except Exception as e:
        driver.implicitly_wait(10)
        break

    driver.implicitly_wait(10)
